package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.MainRVAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.GoodsBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.view.IRecyclerView;
import com.meihua.baoleyi.view.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 搜索界面
 */

public class SearchActivity extends BaseActivity implements MainRVAdapter.OnItemClickListener {

    @Bind(R.id.et_search)
    EditText etSearch;//搜索
    @Bind(R.id.ins_iRecyclerView)
    IRecyclerView mRecyclerView;
    @Bind(R.id.tv_back)
    ImageView tvBack; //返回
    @Bind(R.id.tv_cancle)
    TextView tvCancle; //取消
    @Bind(R.id.tv_tip)
    TextView tvTip; //提示
    private int mPage = 1;
    private ArrayList<GoodsBean.DataBean> mData;
    private MainRVAdapter rvAdapter;
    public LoadMoreFooterView mLoadMoreFooterView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        etSearch.setOnEditorActionListener(onEditorActionListener);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        if (rvAdapter == null) {
            mData = new ArrayList<>();
            rvAdapter = new MainRVAdapter(this, mData);
            rvAdapter.setOnItemClickListener(this);
            mRecyclerView.setIAdapter(rvAdapter);
        } else {
            rvAdapter.notifyDataSetChanged();
        }
        /*设置iv*/
        mLoadMoreFooterView = (LoadMoreFooterView) mRecyclerView.getLoadMoreFooterView();

    }

    /*输入法搜索监听*/
    private TextView.OnEditorActionListener onEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                getServerData(textView.getText().toString());
                /*软件盘消失*/
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                return true;
            }
            return false;
        }
    };

    /*搜索*/
    private void getServerData(final String text) {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("goods_name", text);
            }
        };
        NetHelper.post(APIServer.SEARCH, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                GoodsBean bean = NetHelper.getBeanForJson(GoodsBean.class, s);
                if (bean == null) return;
                if (null != bean.getData()) {
                    if (bean.getData().size() != 0) {
                        tvTip.setVisibility(View.GONE);
                        if (mPage == 1) {
                            /*刷新*/
                            mRecyclerView.setRefreshing(false);
                            mData.clear();
                        }
                        mData.addAll(bean.getData());
                        rvAdapter.notifyDataSetChanged();
                    } else {
                        tvTip.setVisibility(View.VISIBLE);
                        mData.clear();
                        rvAdapter.notifyDataSetChanged();
                        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                    }
                } else {
                    mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                }
                dismissDialog();
            }
        });
    }

    @Override
    public void onItemClick(GoodsBean.DataBean bean) {
        Intent intent = new Intent(SearchActivity.this, CommonWebActivity.class);
        intent.putExtra("url", APIServer.GOODSDETAIL + MyApp.getInstance().getToken() + "/" + MyApp.getInstance().getUser_id() + "/" + bean.getGoods_id());
        startActivity(intent);
        finish();
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.tv_back, R.id.tv_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_cancle:
                finish();
                break;
        }
    }
}
